<?php

namespace App\Admin\Renderable;

use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;
use Faker\Factory;
use App\Models\Part;
use App\Models\PartCategory;

class PartTable extends LazyRenderable
{
    public function render()
    {

         $id = $this->key;
         $type = $this->Asset_Number;
         $data = part::where('Owning_device',$id)
          ->get(['Parts_classification',
                 'name',
                 'specification',
                 'asset_number',
                 'state'])
         ->map(function ($item) {
            $item['Parts_classification'] = PartCategory::find($item['Parts_classification'])->PartCategory;
                    return $item;
         })
         ->map(function ($item) {
            $item['asset_number'] = '<span style="background-color: pink; color: black;">'. $item['asset_number']. '</span>';
                    return $item; 
         })
         ->map(function ($item) {
            if ($item['state'] == '正常') {
                $item['state'] = '<span style="background-color: green; color: white;">' . $item['state'] . '</span>';
            }
            if ($item['state'] == '空闲') {
                $item['state'] = '<span style="background-color: black; color: white;">' . $item['state'] . '</span>';
            }
            if ($item['state'] == '报废') {
                $item['state'] = '<span style="background-color: red; color: white;">' . $item['state'] . '</span>';
            }
            return $item;
        })
     
        ->toArray();


         $titles = [
            '分类',
            '名称',
            '规格',
            '资产编号',
            '状态'
        ];


      return Table::make($titles, $data);


    }
}